﻿using System;
using System.Collections.Generic;
using System.Text;

using Jim.Obsidian.Edm;

namespace Jim.Obsidian.Web.Api.Client
{
    public class ApiObject
    {

        private Dictionary<string, object> values = new Dictionary<string, object>();


        public void Set(string name, object value)
        {
            values.Add(name, value);
        }

        public object GetValue(string name)
        {
            return values[name];
        }

        public string GetString(string name)
        {
            return Convert.ToString(values[name]);
        }

        public int GetInt(string name)
        {
            return Convert.ToInt32(values[name]);
        }

        public long GetLong(string name)
        {
            return Convert.ToInt64(values[name]);
        }

        public decimal GetDecimal(string name)
        {
            return Convert.ToDecimal(values[name]);
        }

        public bool GetBool(string name)
        {
            return Convert.ToBoolean(values[name]);
        }

        public DateTime GetDateTime(string name)
        {
            return Convert.ToDateTime(values[name]);
        }

        public T GetModel<T>() where T: Model, new()
        {
            T t = new T();
            foreach (KeyValuePair<string, object> pair in values)
            {
                IModelField field = t.GetFieldByJPropName(pair.Key);
                if (field != null)
                    field.Set(pair.Value);
            }
            return t;
        }
        

    }
}
